<template>
	<view>
		<view>
			<u-cell-group  title="主页通知 ·">
				<u-gap height="20" bg-color="white"></u-gap>
				<view v-for="(item,index) in dataList" :key="index">
					<u-cell-item icon="bell" :title="item.title" :arrow="false" value="删除" @click="deleteNotice(item)"></u-cell-item>
				</view>
			</u-cell-group>
		</view>
		<view>
			<u-modal v-model="show" :content="content"></u-modal>
			<u-modal v-model="sshow" :content="scontent" show-cancel-button="true" @confirm="deleteInfo()"></u-modal>
			<u-modal v-model="ssshow" :content="sscontent" show-cancel-button="true" @confirm="deleteAllNotice()"></u-modal>
			<view class="btn">
				<u-button class="u-m-r-5" @click="deleteAll()" plain type="error">删除全部</u-button>
			</view>
		</view>
	</view>
</template>

<script>
	import api  from '../../utils/api.js'
	export default {
		data() {
			return {
				dataList:[],
				show:false,
				sshow:false,
				ssshow:false,
				content:'',
				scontent:'',
				sscontent:'',
				obj:[]
			}
		},
		created(){
			this.requestData()
		},
		methods: {
			requestData(){
				let user = uni.getStorageSync("user");
				//console.log(user)
				api.getNoticeList(user.branchId,this.globalConst.INDEX_NOTIFICATION_TYPE).then(res =>{
					console.log(res)
					let data = res.data
					this.dataList = data
				})
			},
			deleteNotice:function(item){
				this.obj = item
				this.sshow = true
				this.scontent = "确定要删除吗"
			},
			deleteInfo:function(){
				api.deleteIndexNotice(this.obj.id).then(res => {
					if (res.code === 0){
						this.show = true
						this.content = "删除成功"
						this.requestData()
					}else{
						this.show = true
						this.content = "系统错误"
					}
				})
			},
			deleteAll:function(){
				this.ssshow = true
				this.sscontent = "确定要删除全部通知吗？"
			},
			deleteAllNotice:function(){
				if (this.dataList.length === 0){
					this.show = true
					this.content = "通知为空"
				}
				for (var i = 0;i < this.dataList.length;i++){
					api.deleteIndexNotice(this.dataList[i].id).then(res => {})
				}
				this.requestData()
				this.show = true
				this.content = "删除成功"
			}
		}
	}
</script>

<style>
.btn{
		margin: 0 auto;
		width: 60%;
		margin-top: 30rpx;
		
	}
</style>
